package com.changgou.service.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.common.pojo.IdWorker;
import com.changgou.goods.pojo.*;
import com.changgou.service.goods.dao.*;
import com.changgou.service.goods.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author tu zy
 * @version 1.0
 * @date 2021/6/20 22:47
 */
@Service
public class SpuServiceImpl implements SpuService {

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private SpecMapper specMapper;

    /**
     * 根据SpuId查询商品
     * @param spuId
     * @return
     */
    @Override
    public Goods findGoodsById(Long spuId) {

        Spu spu = spuMapper.selectByPrimaryKey(spuId);
        Sku sku = new Sku();
        sku.setSpuId(String.valueOf(spuId));
        List<Sku> skuList = skuMapper.select(sku);

        Goods goods = new Goods();

        goods.setSpu(spu);
        goods.setSkuList(skuList);

        return goods;
    }

    /**
     * 添加商品
     * @param goods
     */
    @Override
    public void saveGoods(Goods goods) {
        //spu一个
        Spu spu = goods.getSpu();

        //判断Spu的Id是否为空
        if(spu.getId()==null){
            //为空则为增加
            spu.setId(String.valueOf(idWorker.nextId()));
            spuMapper.insertSelective(spu);
        }else{
            //否则修改
            spuMapper.updateByPrimaryKeySelective(spu);

            //删除之前的List<Sku>
            Sku sku = new Sku();
            sku.setSpuId(spu.getId());
            skuMapper.delete(sku);
        }
        Date date = new Date();

        //查询三级分类名称
        Category category = categoryMapper.selectByPrimaryKey(spu.getCategory3Id());
        String categoryName = category.getName();

        //查询品牌名
        Brand brand = brandMapper.selectByPrimaryKey(spu.getBrandId());
        String brandName = brand.getName();


        //Sku  ---   list
        List<Sku> skuList = goods.getSkuList();
        for (Sku sku:skuList) {
            sku.setId(String.valueOf(idWorker.nextId()));

            String name = spu.getName();

            //防止空指针
            if (StringUtils.isEmpty(sku.getSpec())){
                sku.setSpec("{}");
            }

            //将spec转成Map
            Map<String,String> specMap = JSON.parseObject(sku.getSpec(), Map.class);
            for (Map.Entry<String,String> entry:specMap.entrySet()) {
                name+="" + entry.getValue();
            }
            sku.setName(name);//spu.Name+规格
            sku.setCreateTime(date);
            sku.setUpdateTime(date);
            sku.setSpuId(spu.getId());
            sku.setCategoryId(spu.getCategory3Id());
            sku.setCategoryName(categoryName);
            sku.setBrandName(brandName);

            //将Sku添加到数据库
            skuMapper.insertSelective(sku);
        }
    }

    /**
     * 商品审核
     * @param spuId
     */
    @Override
    public void audit(Long spuId) {
        Spu spu = spuMapper.selectByPrimaryKey(spuId);

        if (spu.getIsDelete().equalsIgnoreCase("1")){
            throw new RuntimeException("改商品已经被删除");
        }

        //实现审核和上架
        spu.setStatus("1");
        spu.setIsMarketable("1");
        spuMapper.updateByPrimaryKeySelective(spu);

    }

    /**
     * 商品下架
     * @param spuId
     */
    @Override
    public void pull(Long spuId) {
        Spu spu = spuMapper.selectByPrimaryKey(spuId);

        if (spu.getIsDelete().equalsIgnoreCase("1")){
            throw new RuntimeException("改商品已经被删除");
        }

        //实现商品下架
        spu.setIsMarketable("0");

        spuMapper.updateByPrimaryKeySelective(spu);

    }


    /**
     * 商品上架
     * @param spuId
     */
    @Override
    public void put(Long spuId) {
        Spu spu = spuMapper.selectByPrimaryKey(spuId);

        if (spu.getIsDelete().equalsIgnoreCase("1")){
            throw new RuntimeException("商品已经被删除");
        }

        if (!spu.getStatus().equalsIgnoreCase("1")){
            throw new RuntimeException("商品审核未通过");
        }

        //商品上架
        spu.setIsMarketable("1");

        spuMapper.updateByPrimaryKeySelective(spu);

    }

    /**
     * 批量上架
     * @param spuIds
     */
    @Override
    public int putMany(Long[] spuIds) {
        //错误思路
        /*for (Long spuId:spuIds) {
            Spu spu = spuMapper.selectByPrimaryKey(spuId);

            if (spu.getIsDelete().equalsIgnoreCase("1")){
                throw new RuntimeException(spuId+"商品已经被删除");
            }

            if (!spu.getStatus().equalsIgnoreCase("1")){
                throw new RuntimeException(spuId+"商品审核未通过");
            }

            //商品上架
            spu.setIsMarketable("1");
            spuMapper.updateByPrimaryKeySelective(spu);*/

        Spu spu = new Spu();

        //上架
        spu.setIsMarketable("1");

        //update tb_sku set IsMarketable=1 where id in(ids) and isdelete=0 status;
        //批量修改
        Example example = new Example(Spu.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andIn("id", Arrays.asList(spuIds));

        //未删除
        criteria.andEqualTo("isDelete","0");

        //已审核
        criteria.andEqualTo("status","1");

        return spuMapper.updateByExampleSelective(spu,example);

    }









}
